package tuvalLib;
import astroLib.*;
import java.io.IOException;
import javax.microedition.lcdui.*;
import lang.LocalizationSupport;
//import record.OptionPrefer;
public class CompassCanvas extends Canvas implements CommandListener {

    private Image image;
    private SunMoonPosition mSunMoonPosition;
    private String locationName;
    private byte screenTop;
    private Settings preferences;
    private EarthPosition position;
    public CompassCanvas() {
    
     //this.setFullScreenMode(true);
     
    }
    private void setmSunMoonPosition (SunMoonPosition mSunMoonPosition)
    {
        this.mSunMoonPosition=mSunMoonPosition;
    }
      private void setScreenTopPoint (byte screenTop)
    {
        this.screenTop=screenTop;
    }

   public void setPreferences(Settings preferences) {
        this.preferences=preferences;
        this.position = new EarthPosition(Settings.locationPrefer.getLatitude(), Settings.locationPrefer.getLongitude());
        this.mSunMoonPosition=new SunMoonPosition (position);
        this.locationName =Settings.locationPrefer.getLocation();
        this.setFullScreenMode(Settings.optionPref.getIsFullScreen());
        setScreenTopPoint(Settings.optionPref.getScreenTop());

    }
    public void paint(Graphics g) {
        synchronized (g) {
            int midX = getWidth() / 2;
            int midY = getHeight() / 2;
             //mSunMoonPosition=getSunMoonPosition();
            //System.out.println("getWidth()"+getWidth()+"getHeight()"+getHeight());
            int maxR = Math.min(midX, midY);
            int i;
            for (i = 6; i > 0; i--) {
                int r = maxR * i / 6;
                if (i == 6) {
                    if (mSunMoonPosition == null || mSunMoonPosition.getSunPosition().getElevation() > -10) {
                        g.setColor(0xFFFFFF);
                        g.fillRect(0, 0, getWidth(), getHeight());
                        g.setColor(0x0080ff);
                    } else {
                        g.setColor(0x000000);
                        g.fillRect(0, 0, getWidth(), getHeight());
                        g.setColor( 0x808080);
                    }
                    g.fillArc(midX - r, midY - r, r * 2, r * 2, 0, 360);
                    g.setColor(g.getRedComponent()/2, g.getGreenComponent()/2, g.getBlueComponent()/2);
                }
                g.drawArc(midX - r, midY - r, r * 2, r * 2, 0, 360);
            }
            Horizontal lineSkyPosition = new Horizontal(0, 0);
            int step;
            int azimuthOffset = 0;
            if (mSunMoonPosition != null) {
                Horizontal sunPosition = mSunMoonPosition.getSunPosition();
               // System.out.println("Az="+sunPosition.Az+"h"+sunPosition.h);
                Horizontal moonPosition = mSunMoonPosition.getMoonPosition();
                EarthHeading destinationHeading = mSunMoonPosition.getDestinationHeading();
                switch (screenTop) {
                    case 0: // North up
                        break;
                    case 1: // Sun or Moon up
                        if (sunPosition.getElevation() > -10)
                            azimuthOffset = (int)sunPosition.getAzimuth();
                        else if (moonPosition.getElevation() > -5)
                            azimuthOffset = (int)moonPosition.getAzimuth();
                        break;
                    case 2: // Destination up
                        if (destinationHeading != null)
                            azimuthOffset =(int) destinationHeading.getHeading();
                        break;
                }
            }
            boolean flipX =false;
            for (step = 0; step < 8; step++) {
                lineSkyPosition.setAzimuth(step * 45);
                ScreenPosition lineScreenPosition = lineSkyPosition.toScreenPosition(this, azimuthOffset, flipX);
                g.drawLine(midX, midY, lineScreenPosition.x, lineScreenPosition.y);
            }
            Font labelFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
            g.setFont(labelFont);
            Horizontal labelSkyPosition = new Horizontal(0, 10);
            String labels[] = {LocalizationSupport.getMessage("NORTH"), LocalizationSupport.getMessage("EAST"), LocalizationSupport.getMessage("SOUTH"), LocalizationSupport.getMessage("WEST")};
            int radius = Math.max(labelFont.charWidth('W'), labelFont.getHeight())/2;
            g.drawString(LocalizationSupport.getMessage("CITY")+" "+locationName,midX, midY/50, Graphics.TOP|Graphics.HCENTER);
            for (i = 0; i < labels.length; i++) {
                labelSkyPosition.setAzimuth(i * 90);
                ScreenPosition label = labelSkyPosition.toScreenPosition(this, azimuthOffset, flipX);
                g.setColor(0x008080);
                g.fillRoundRect(label.x - radius, label.y - radius, radius * 2, radius * 2, 8, 8);
                g.setColor(0x000000);
                g.drawString(labels[i], label.x, label.y - labelFont.getHeight()/2, g.TOP|g.HCENTER);
            }
          
                Horizontal sunPosition = mSunMoonPosition.getSunPosition();
                Horizontal moonPosition = mSunMoonPosition.getMoonPosition();
                double moonPhase = mSunMoonPosition.getMoonPhase();
                EarthHeading destinationHeading = mSunMoonPosition.getDestinationHeading();
                if (sunPosition.getElevation() <= -10 && moonPosition.getElevation() <= -5) {
                    g.drawString(LocalizationSupport.getMessage("NOSUN"), midX, midY, g.BOTTOM|g.HCENTER);
                    g.drawString(LocalizationSupport.getMessage("ORMOON"), midX, midY, g.TOP|g.HCENTER);
                }
                int r = Math.max(maxR / 6, 8);
                int d = r * 2;
                if (moonPosition.getElevation() > -5) {
                    ScreenPosition moon = moonPosition.toScreenPosition(this, azimuthOffset, flipX);
                    Horizontal line = new Horizontal(moonPosition.getAzimuth(), 0);
                    ScreenPosition line1 = line.toScreenPosition(this, azimuthOffset, flipX);
                    line.setAzimuth(line.getAzimuth() + 180);
                    ScreenPosition line2 = line.toScreenPosition(this, azimuthOffset, flipX);
                    g.setStrokeStyle(g.DOTTED);
                    g.drawLine(line1.x, line1.y, line2.x, line2.y);
                    g.setStrokeStyle(g.SOLID);
                    g.setColor(0xffffff);
                    g.fillArc(moon.x - r, moon.y - r, d, d, 90, 180);
                    g.setColor(0x000000);
                    g.fillArc(moon.x - r, moon.y - r, d, d, 270, 180);
                    //int arcWidth = (int)(moonPhase.Sub(new Float(5, -1)).Mul(d * 2).toLong());
                   // int arcWidth = (int)(moonPhase.Sub(new Float(5, -1)).Mul(d * 2).toLong());
                    int arcWidth = (int)((moonPhase-0.5)*(2*d));
                    //System.out.println("moonPhase"+moonPhase+" Arc"+arcWidth+" r "+r );
                    g.setColor(arcWidth < 0 ? 0x000000 : 0xffffff);
                    g.fillArc(moon.x - Math.abs(arcWidth)/2, moon.y - r, Math.abs(arcWidth), d, 0, 360);
                    //g.drawArc(moon.x - Math.abs(arcWidth)/2, moon.y - r, Math.abs(arcWidth), d, 0, 360);
                    g.setColor(0x000000);
                    g.drawArc(moon.x - r, moon.y - r, d, d, 0, 360);





                }
                if (sunPosition.getElevation() > -10) {
                    ScreenPosition sun = sunPosition.toScreenPosition(this, azimuthOffset, flipX);
                    Horizontal line = new Horizontal(sunPosition.getAzimuth(), 0);
                    ScreenPosition line1 = line.toScreenPosition(this, azimuthOffset, flipX);
                    line.setAzimuth(line.getAzimuth()+ 180);
                    ScreenPosition line2 = line.toScreenPosition(this, azimuthOffset, flipX);
                    g.setStrokeStyle(g.DOTTED);
                    g.drawLine(line1.x, line1.y, line2.x, line2.y);
                    g.setStrokeStyle(g.SOLID);
                    g.setColor(0xffff00);
                    g.fillArc(sun.x - r, sun.y - r, d, d, 0, 360);
                    g.setColor(0x000000);
                    g.drawArc(sun.x - r, sun.y - r, d, d, 0, 360);
                }
                if (destinationHeading != null) {
                
                    g.setColor(65535);
                    Horizontal destinationSkyPosition = new Horizontal(destinationHeading.getHeading(), 0);
                    ScreenPosition line1 = destinationSkyPosition.toScreenPosition(this, azimuthOffset, flipX);
                    destinationSkyPosition.setAzimuth(destinationSkyPosition.getAzimuth() + 180);
                    ScreenPosition line2 = destinationSkyPosition.toScreenPosition(this, azimuthOffset, flipX);
                    g.setStrokeStyle(g.DOTTED);
                    g.drawLine(line1.x, line1.y, line2.x, line2.y);
                    destinationSkyPosition.setElevation(10);
                    destinationSkyPosition.setAzimuth(destinationHeading.getHeading());
                    ScreenPosition labelPosition = destinationSkyPosition.toScreenPosition(this, azimuthOffset, flipX);
                  //  g.fillRoundRect(labelPosition.x - radius, labelPosition.y - radius, radius * 2, radius * 2, 8, 8);
                    g.setColor(0);
                    try {
                    image=null;    
                    image = Image.createImage("/res/icon/kaba.png");
                    ///image.getGraphics ();
                    g.drawImage(image, labelPosition.x, labelPosition.y - labelFont.getHeight()/2,g.TOP|g.HCENTER);
                    } catch (IOException ex) {throw new RuntimeException ("Resim Bulunamiyor: "+ex);}
                              
                }
            }
        }
 /*   public void setLocationName(String locationName) {
        this.locationName = locationName;
    }*/
/*      private SunMoonPosition getSunMoonPosition()
    {
        mSunMoonPosition=new SunMoonPosition (new EarthPosition(Math.toRadians(Settings.locationPrefer.getLatitude()),Math.toRadians(Settings.locationPrefer.getLongitude())));
       //this.mSunMoonPosition=mSunMoonPosition;
        return mSunMoonPosition;
    }*/

    public void commandAction(Command arg0, Displayable arg1) {
    }



    }

  
    
